package objects;

import java.io.IOException;
import objects.visualization.Block;
import objects.visualization.ListBlock;
import objects.visualization.TextBlock;

public class Module extends AddressedObject {
	public Method method;
	public GruObject object;
	public DataObject[] parameters;

	public Module() {
		this.gruClass = moduleClass;
	}
	
	@Override
	public void io() throws IOException {
		method = objectIO( method );
		object = objectIO( object );
		parameters = arrayIO( parameters, method.parameters.size() );
	}
	
	@Override
	public DataObject newObject() {
		return new Module();
	}
	
	@Override
	public String toCode( String indent ) {
		 String params = "";
		 for( DataObject obj : parameters ) {
			 params += obj.toCode( "" ) + ( params.isEmpty() ? "" : ", " );
		 }
		 return indent + headerCode() + "( " + params + " )";
	}

	@Override
	public Block toModuleBlock( int indent ) {
		ListBlock block = new ListBlock( false, true );
		block.add( new TextBlock( this, headerCode() + "( ", 1 ) );
		boolean first = true;
		for( DataObject parameter : parameters ) {
			if( !first ) block.add( new TextBlock( null, ", " ) );
			block.add( parameter.toModuleBlock( 0 ) );
			first = false;
		}
		block.add( new TextBlock( null, " )" ) );
		return block;
	}

	private String headerCode() {
		return ( object == null ? "" : object.toCode( "" ) + "." ) + method.iD;
	}
}
